package com.jxc.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jxc.dao.BaseDaoI;
import com.jxc.model.po.Checklist;
import com.jxc.model.po.ChecklistItem;
import com.jxc.model.po.Goods;
import com.jxc.model.vo.DataGrid;
import com.jxc.model.vo.VChecklistItem;
import com.jxc.service.ChecklistItemServiceI;

@Service("checklistItemService")
public class ChecklistItemServiceImpl implements ChecklistItemServiceI {

	@Autowired
	private BaseDaoI<ChecklistItem> checklistItemDao;

	@Override
	public List<VChecklistItem> combobox(VChecklistItem vcli) {
		return this.changeModel(this.find(vcli));
	}

	@Override
	public DataGrid datagrid(VChecklistItem vcli) {
		DataGrid dg = new DataGrid();
		dg.setRows(this.changeModel(this.find(vcli)));
		dg.setTotal(this.total(vcli));
		return dg;
	}

	@Override
	public void delete(String ids) {
		if (ids != null) {
			for (String id : ids.split(",")) {
				if (!id.trim().equals("0")) {
					ChecklistItem cli = checklistItemDao.get(ChecklistItem.class, Long.parseLong(id.trim()));
					if (cli != null) {
						checklistItemDao.delete(cli);
					}
				}
			}
		}
	}

	@Override
	public void reset(VChecklistItem vcli) {
		List<ChecklistItem> clis = this.find(vcli);
		for (ChecklistItem cli : clis) {
			cli.setIsChecked(false);
			cli.setCheckDate(null);
			cli.setCheckAmount(0);
			checklistItemDao.update(cli);
		}
	}

	@Override
	public void save(VChecklistItem vcli) {
		ChecklistItem cli = new ChecklistItem();
		BeanUtils.copyProperties(vcli, cli);
		if (vcli.getGid() != null) {
			cli.setGoods(new Goods(vcli.getGid()));
		}
		if (vcli.getClid() != null) {
			cli.setChecklist(new Checklist(vcli.getClid()));
		}
		this.checklistItemDao.save(cli);
	}

	@Override
	public void update(VChecklistItem vcli) {
		ChecklistItem cli = checklistItemDao.get(ChecklistItem.class, vcli.getId());
		BeanUtils.copyProperties(vcli, cli, new String[] { "id" });
		if (vcli.getGid() != null) {
			cli.setGoods(new Goods(vcli.getGid()));
		}
		if (vcli.getClid() != null) {
			cli.setChecklist(new Checklist(vcli.getClid()));
		}
		this.checklistItemDao.update(cli);
	}

	private List<VChecklistItem> changeModel(List<ChecklistItem> clis) {
		List<VChecklistItem> vclis = new ArrayList<VChecklistItem>();
		if (clis != null && clis.size() > 0) {
			for (ChecklistItem cli : clis) {
				VChecklistItem vcli = new VChecklistItem();
				BeanUtils.copyProperties(cli, vcli);
				if (cli.getGoods() != null) {
					Goods goods = cli.getGoods();
					vcli.setGid(goods.getId());
					vcli.setGname(goods.getName());
					
					vcli.setInitAmount(goods.getInitAmount());
					vcli.setOrderTotal(goods.getOrderTotal());
					vcli.setOrderReturnTotal(goods.getOrderReturnTotal());
					vcli.setSaleTotal(goods.getSaleTotal());
					vcli.setSaleReturnTotal(goods.getSaleReturnTotal());
					vcli.setDamageTotal(goods.getDamageTotal());
					vcli.setTransferTotal(goods.getTransferTotal());
					vcli.setRepertoryTotal(goods.getRepertoryTotal());
					vcli.setAmount(goods.getAmount());
				}
				if (cli.getChecklist() != null) {
					Checklist cl = cli.getChecklist();
					vcli.setClid(cl.getId());
				}
				vclis.add(vcli);
			}
		}
		return vclis;
	}

	private List<ChecklistItem> find(VChecklistItem vcli) {
		String hql = "from ChecklistItem t where 1=1 ";

		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vcli, hql, values);

		if (vcli.getSort() != null && vcli.getOrder() != null) {
			hql += " order by " + vcli.getSort() + " " + vcli.getOrder();
		}
		return checklistItemDao.find(hql, values, vcli.getPage(), vcli.getRows());
	}

	private Long total(VChecklistItem vcli) {
		String hql = "select count(*) from ChecklistItem t where 1=1 ";
		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vcli, hql, values);
		return checklistItemDao.count(hql, values);
	}

	private String addWhere(VChecklistItem vcli, String hql, List<Object> values) {
		if (vcli.getId() != null) {
			hql += " and t.id = ? ";
			values.add(vcli.getId());
		}
		if (vcli.getGid() != null) {
			hql += " and t.goods.id = ? ";
			values.add(vcli.getGid());
		}
		if (vcli.getClid() != null) {
			hql += " and t.checklist.id = ? ";
			values.add(vcli.getClid());
		}
		return hql;
	}

}
